//
//  ABQiniuUtils.m
//  AiBa
//
//  Created by Stan Wu on 14-3-22.
//
//

#import "ABQiniuUtils.h"
#import "NSData+Base64.h"
#import "SWDataToolKit.h"



#define kQiniuAccessKey @"D0lZGP7vV5AaW67bthv855GP6dUItWv2fylhaTkM"
#define kQiniuSecretKey @"EIfPlG45ps01ep8eo88NkY5EqYbMx8x-db1xT14y"

#define kAiBaQiniuAccessKey @"O6CdPaToCBcrmE5tZsz7HK8Ck4qia7XwNTFQ0ELK"
#define kAiBaQiniuSecretKey @"rv5TBbZTNeQBUNzGa_2k3yBoV6kTuZuTW6ihhT5W"

#define  kHaiMaQiniuAccesskey @"xhAbCfcK0NOYvthRnxJ7j-Nduq2b6wOLuuONZuVa"
#define  kHaiMaQiniuSecretKey @"xb2mtw2Ro9BbWut1KLfQJYjz4j5lpVc6jtFTU_2b"

@implementation ABQiniuUtils

+ (NSString *)uploadTokenOfBucket:(NSString *)bucket name:(NSString *)fileName{
    NSNumber *deadline = [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]+60*60];

    NSString *scope = fileName?[NSString stringWithFormat:@"%@:%@",bucket,fileName]:bucket;
    
    NSDictionary *dicPolity = [NSDictionary dictionaryWithObjectsAndKeys:scope,@"scope",deadline,@"deadline", nil];
    
//    NSString *putPolicy = @"{\"deadline\":1451491200,\"scope\":\"aiba-auth-video\"}";
    
    NSString *encodedPutPolicy = [[NSJSONSerialization dataWithJSONObject:dicPolity options:0 error:nil] urlsafe_base64_encode];
    NSData *sign = [encodedPutPolicy HMACSHA1WithKey:kQiniuSecretKey];
    

    NSString *encodedSign = [sign urlsafe_base64_encode];
    

    
    return [NSString stringWithFormat:@"%@:%@:%@",kQiniuAccessKey,encodedSign,encodedPutPolicy];
}

+ (NSString *)uploadTokenOfFile:(NSString *)fileName{
    NSNumber *deadline = [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]+60*60];
    
    NSString *scope = fileName?[NSString stringWithFormat:@"haima-media:%@",fileName]:@"haima-media";
    
    NSDictionary *dicPolity = [NSDictionary dictionaryWithObjectsAndKeys:scope,@"scope",deadline,@"deadline", nil];
    
    //    NSString *putPolicy = @"{\"deadline\":1451491200,\"scope\":\"aiba-auth-video\"}";
    
    NSString *encodedPutPolicy = [[NSJSONSerialization dataWithJSONObject:dicPolity options:0 error:nil] urlsafe_base64_encode];
    
    
   
    NSData *sign = [encodedPutPolicy HMACSHA1WithKey:@"xb2mtw2Ro9BbWut1KLfQJYjz4j5lpVc6jtFTU_2b"];
    NSString *encodedSign = [sign urlsafe_base64_encode];
    
    NSString *str = [NSString stringWithFormat:@"%@:%@:%@",kHaiMaQiniuAccesskey,encodedSign,encodedPutPolicy];
    
    return str;
}

+ (NSString *)mediaToken:(NSString *)fileName{
    NSNumber *deadline = [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]+60*60];
    
    NSString *scope = fileName?[NSString stringWithFormat:@"haima-media:%@",fileName]:@"haima-media";
    
    NSDictionary *dicPolity = [NSDictionary dictionaryWithObjectsAndKeys:scope,@"scope",deadline,@"deadline", nil];
    
    //    NSString *putPolicy = @"{\"deadline\":1451491200,\"scope\":\"aiba-auth-video\"}";
    
    NSString *encodedPutPolicy = [[NSJSONSerialization dataWithJSONObject:dicPolity options:0 error:nil] urlsafe_base64_encode];
    NSData *sign = [encodedPutPolicy HMACSHA1WithKey:kHaiMaQiniuSecretKey];
    NSString *encodedSign = [sign urlsafe_base64_encode];
    
    
    
    return [NSString stringWithFormat:@"%@:%@:%@",kHaiMaQiniuAccesskey,encodedSign,encodedPutPolicy];
}

@end
